package com.zangyi.service.impl;

import com.zangyi.common.*;
import com.zangyi.common.Collection;
import com.zangyi.mapper.CollectionMapper;
import com.zangyi.mapper.CommentMapper;
import com.zangyi.mapper.ForwardMapper;
import com.zangyi.mapper.UserMapper;
import com.zangyi.service.CommentService;
import org.apache.juli.logging.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    CollectionMapper collectionMapper;
    @Autowired
    ForwardMapper forwardMapper;

    @Override
    public boolean addComment(String username, String title, String comment) {
        Comment ObComment = new Comment(username, title, comment, new Date());
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUsernameEqualTo(username);
        List<User> users = userMapper.selectByExample(userExample);
        try {
            ObComment.setHeadPicture(users.get(0).getHeadpicture());
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(("集合越界，可能未查到用户"));
        }
        return commentMapper.insertSelective(ObComment) == 1 ? true : false;
    }

    @Override
    public void delComment(Integer comId) {
        commentMapper.deleteByPrimaryKey(comId);
    }

    @Override
    public List<Map<String, Object>> getTitleComment(String title, String username) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        CommentExample commentExample = new CommentExample();
        commentExample.createCriteria().andTitleEqualTo(title);
        List<Comment> comments = commentMapper.selectByExample(commentExample);
        if ("null".equals(username)) {
            Map<String, Object> map = new HashMap<>();
            for (int i = 0; i < comments.size(); i++) {
                map.put("isSelf", 0);
                map.put("isCol", 0);
                map.put("comment", comments.get(i));
                list.add(map);
            }
        } else {
            for (int i = 0; i <comments.size() ; i++) {
                Map<String, Object> map = new HashMap<>();
                CollectionExample collectionExample =new CollectionExample();
                collectionExample.createCriteria().andUsernameEqualTo(username).andComIdEqualTo(comments.get(i).getComId());
                List<Collection> collections = collectionMapper.selectByExample(collectionExample);
                if(collections.size()>0)
                    map.put("isCol",1);
                else
                    map.put("isCol",0);
                if(username.equals(comments.get(i).getUsername()))
                    map.put("isSelf",1);
                else
                    map.put("isSelf",0);
                map.put("comment",comments.get(i));
                list.add(map);
            }
        }
        return list;
    }

    @Override
    public List<Comment> getUserComments(String username) {
        CommentExample commentExample = new CommentExample();
        commentExample.createCriteria().andUsernameEqualTo(username);
        return commentMapper.selectByExample(commentExample);
    }

    @Override
    public boolean collectionComment(Integer comId, String username) {
        /*避免重复收藏*/
        CommentExample commentExample = new CommentExample();
        commentExample.createCriteria().andComIdEqualTo(comId);
        List<Comment> comments = commentMapper.selectByExample(commentExample);
        if (comments.size() != 0) return false;

        Comment comment = commentMapper.selectByPrimaryKey(comId);
        Collection collection = new Collection(comment.getComId(), comment.getTitle(), comment.getComment(), comment.getUsername(), username);
        return collectionMapper.insertSelective(collection) == 1 ? true : false;
    }

    @Override
    public void delCollection(Integer colId) {
        collectionMapper.deleteByPrimaryKey(colId);
    }

    @Override
    public void toForward(Integer comId, String username) {
        Forward forward =new Forward(comId,username);
        forwardMapper.insertSelective(forward);
    }
}
